package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BookStoreDataBean;

import java.util.List;

/**
 * Created by Hyy on 2016/12/21.
 */

public class FragmentBookStoreRVAdapter extends CommonRecycleViewAdapter<BookStoreDataBean.GoodsListBean> {
    Context context;

    public FragmentBookStoreRVAdapter(Context mContext, List<BookStoreDataBean.GoodsListBean> itemDatas) {
        super(mContext, R.layout.fragment_bookstore_rv_item, itemDatas);
        this.context = mContext;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final BookStoreDataBean.GoodsListBean item, int position) {
        View view = customViewHold.getView(R.id.flag);
        view.setBackgroundColor(Color.parseColor(item.getTitle().getTitleColor()));
        TextView title = customViewHold.getView(R.id.title);
        title.setText(item.getTitle().getName());

        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        if (item.getBanner() != null && !TextUtils.isEmpty(item.getBanner().getImg())) {
            simpleDraweeView.setImageURI(item.getBanner().getImg());

            simpleDraweeView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, MallCommonH5Activity.class);
                    intent.putExtra(MallCommonH5Activity.URL_KEY, item.getBanner().getLink());
                    context.startActivity(intent);
                }
            });
        } else {
            simpleDraweeView.setVisibility(View.GONE);
        }


        RecyclerView recyclerView = customViewHold.getView(R.id.recyclerView);
        GridLayoutManager manager = new GridLayoutManager(context, 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new RootItemAdapter(context, item.getGoods()));
    }

    class RootItemAdapter extends CommonRecycleViewAdapter<BookStoreDataBean.GoodsListBean.GoodsBean> {

        public RootItemAdapter(Context mContext, List<BookStoreDataBean.GoodsListBean.GoodsBean> itemDatas) {
            super(mContext, R.layout.fragment_bookstore_rv_root_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, final BookStoreDataBean.GoodsListBean.GoodsBean item, int position) {
            SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
            simpleDraweeView.setImageURI(item.getImg());

            TextView title = customViewHold.getView(R.id.title);
            title.setText(item.getTitle());

            TextView price = customViewHold.getView(R.id.price);
            price.setText("¥" + item.getPrice());

            customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, MallCommonH5Activity.class);
                    intent.putExtra(MallCommonH5Activity.URL_KEY, item.getLink());
                    context.startActivity(intent);
                }
            });
        }
    }
}
